﻿using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Mengsk.ProxyDelivery.App.UI.Order
{
    /// <summary>
    /// Interaction logic for OrderChangeImageViewWindow.xaml
    /// </summary>
    public partial class OrderChangeImageViewWindow : Window
    {
        public string Image1 { get; set; }

        public string Image2 { get; set; }

        public string Image3 { get; set; }

        public long OrderChangeId { get; set; }

        private string currentImage = null;
        private int number = 0;

        public OrderChangeImageViewWindow()
        {
            InitializeComponent();
        }

        private void btnImage1_Click(object sender, RoutedEventArgs e)
        {
            this.SetImage(this.Image1, 1);
        }

        private void btnImage2_Click(object sender, RoutedEventArgs e)
        {
            this.SetImage(this.Image2, 2);
        }

        private void btnImage3_Click(object sender, RoutedEventArgs e)
        {
            this.SetImage(this.Image3, 3);
        }

        private void SetImage(string imageName, int number)
        {
            try
            {
                this.Image.Source = (new Mengsk.ProxyDelivery.Converters.WebUrlImageConverter()).Convert(imageName, null, null, null) as ImageSource;
                this.currentImage = imageName;
                this.number = number;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.SetImage(this.Image1, 1);
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string image = this.currentImage;
                if (string.IsNullOrWhiteSpace(image))
                {
                    MessageBox.Show("没有图片");
                    return;
                }

                Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
                ofd.FileName = this.OrderChangeId + "_" + this.number + image.Substring(image.LastIndexOf(".") + 1);
                bool? ret = ofd.ShowDialog();
                if (ret == null || ret.Value == false)
                {
                    return;
                }
                //Uri uri=new Uri(ServiceContainer.ServerAddress);
                //string url =image.Contains("upload",StringComparison.OrdinalIgnoreCase)? uri.ho ServiceContainer.ServerAddress + "/webservice/image/" + image;
                // Byte[] content = HTTPHelpers.MSHttpRestful.GetUrlEncodeBodyReturnBytes(url, new Dictionary<string, string>());
                // File.WriteAllBytes(ofd.FileName, content);
                //Process.Start(ofd.FileName);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
